// pages/commodity_details/commodity_details.js

import { request } from "../../request/index.js";
import regeneratorRuntime from "../../libs/runtime/runtime";
const app = getApp();

Page({
    /**
     * 页面的初始数据
     */
    data: {
        pics: [], //商品图片
        shoppingData: [], //商品详情数据
        show: false, //默认不显示
        buyNum: 1, //默认选择一件
        radio: "1", //默认第一种支付方式
        // collectType: false, //默认不点亮
        goodsId: "", //商品的id
        bounced: false,
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options.goodsIntegralId);
        this.setData({
            goodsId: options.goodsIntegralId, //赋值商品的id
        });
        this.commodityPost(); //商品详情数据
    },
    //商品详情数据
    async commodityPost() {
        let res = await request({
            url: "GoodsIntegral/findByGoodsIntegralId",
            data: { goodsIntegralId: this.data.goodsId },
        });
        console.log(res);
        if (res.code == 200) {
            this.setData({
                pics: res.data.imgList,
                shoppingData: res.data,
            });
        } else {
            wx.showToast({
                title: res.msg,
                icon: "none",
            });
        }
    },
    // 预览图片
    preview(e) {
        console.log(e);
        // 实现全屏预览的微信API
        wx.previewImage({
            current: e.currentTarget.dataset.url, // 当前显示图片的http链接
            urls: this.data.pics.map((item) => item), // 需要预览的图片http链接列表
            // 因为他期望的是一个数组,我们得到数组里的是对象,所以要用map把里面的对象循环成一个数组,使用map返回的是新数组
        });
    },
    // 收藏按钮
    async collect() {
        if (wx.getStorageSync("token")) {
            let res = await request({
                url: "Collect/update",
                data: {
                    goodId: this.data.goodsId,
                    type: 2,
                },
            });
            console.log(res, "88888888888888");

            if (res.code == 200) {
                this.commodityPost(); //商品详情数据
            }
        } else {
            this.setData({
                bounced: true,
            });
            // app.showModalTitle();
        }
    },
    //领取优惠卷 按钮
    collectionCoupon() {
        if (wx.getStorageSync("token")) {
            wx.navigateTo({
                url: "../collection_coupon/collection_coupon",
            });
        } else {
            this.setData({
                bounced: true,
            });
            // app.showModalTitle();
        }
    },
    onChange(e) {
        console.log(e);
        this.setData({
            radio: e.detail,
        });
    },
    // 加号
    plus() {
        let num = this.data.buyNum;
        num++;
        this.setData({
            buyNum: num,
        });
    },
    // 减号
    delete() {
        let num = this.data.buyNum;
        if (num > 1) {
            num--;
        }
        this.setData({
            buyNum: num,
        });
    },
    // 显示遮罩层
    showBuyModal() {
        console.log("执行; ");

        if (!wx.getStorageSync("token")) {
            // app.showModalTitle();
            this.setData({
                bounced: true,
            });
        } else {
            this.setData({
                show: true,
            });
        }
    },

    // 隐藏遮罩层
    hideBuyModal(e) {
        this.setData({
            show: false,
        });
    },
    // 点击确定按钮
    hideBuyModalAffirm(e) {
        console.log(e);
        this.setData({
            show: false,
        });
        //存储商品的id
        wx.setStorageSync("id", e.target.dataset.id);
        // 存储商品的数量
        wx.setStorageSync("countCredit", e.target.dataset.count);
        wx.removeStorageSync("packingFee");

        console.log(e);
        wx.navigateTo({
            url: "../credit_Confirmation/credit_Confirmation",
        });
    },
    //弹框确认按钮
    getUserInfo(e) {
        console.log(e, "321654987");
        let that = this;
        if (e.detail.userInfo) {
            wx.login({
                success: async(result) => {
                    console.log(result);
                    let res = await request({
                        url: "User/login",
                        method: "post",
                        data: {
                            code: result.code,
                            name: e.detail.userInfo.nickName,
                            img: e.detail.userInfo.avatarUrl,
                        },
                    });
                    console.log(res);
                    if (res.code == 200) {
                        wx.setStorageSync("token", res.data); //存储用户的token
                        that.setData({
                            login: true,
                        });
                        that.ShoppingCartPsot(); //获取购物车数据
                    }
                },
            });
            //存储用户的信息
            wx.setStorageSync("userinfo", e.detail.userInfo);
        } else {
            //用户按了拒绝按钮
            wx.showModal({
                title: "警告",
                content: "您点击了拒绝授权，将无法使用小程序，请授权之后再使用!",
                showCancel: false,
                confirmText: "确定",
                confirmColor: "#3CC51F",
                success: function(res) {
                    // 用户没有授权成功，不需要改变 isHide 的值
                    if (res.confirm) {
                        console.log("用户点击了“返回授权”");
                    }
                },
            });
        }
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {},
});